package nodelist

// https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnwzei/
// 判断链表是否有环
// 算法: 双指针，另一个走的快一些，如果是环，总有重复的时候
// 时间复杂度: O(len)
func hasCycle(head *ListNode) bool {
	if head == nil {
		return false
	}
	slowNode, fastNode := head, head
	for {
		fastNode = fastNode.Next
		if nil == fastNode {
			return false
		}
		fastNode = fastNode.Next
		if nil == fastNode {
			return false
		}
		slowNode = slowNode.Next
		if slowNode == fastNode {
			return true
		}
	}
}
